package com.sc.outpatientsys.controller;

import com.sc.outpatientsys.entity.Pharmaceutist;
import com.sc.outpatientsys.service.IPharmaceutistService;
import com.sc.outpatientsys.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/pharmaceutist")
@Slf4j
public class PharmaceutistController extends BaseController{
    @Autowired
    private IPharmaceutistService pharmaceutistService;
    @RequestMapping("/addPharmaceutist")
    public JsonResult<Void> addPharmaceutist(Pharmaceutist pharmaceutist){
        pharmaceutistService.reg(pharmaceutist);
//        log.info(pharmaceutist.toString());
        return new JsonResult<>(OK);
    }
    @RequestMapping("/login")
    public JsonResult<Pharmaceutist> login(String username, String password, HttpSession session){
        Pharmaceutist data = pharmaceutistService.login(username, password);
        session.setAttribute("id",data.getPh_ID());
        session.setAttribute("username",data.getUserName());
        log.info(getIdFromSession(session).toString());
        log.info(getUserNameFromSession(session));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/findById")
    public JsonResult<Pharmaceutist> findById(int ph_ID){
        Pharmaceutist data = pharmaceutistService.findById(ph_ID);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/findByName")
    public JsonResult<Pharmaceutist> findByName(String username){
        Pharmaceutist data = pharmaceutistService.findByName(username);
        return new JsonResult<>(OK, data);
    }
    @RequestMapping("/findAll")
    public JsonResult<List<Pharmaceutist>> findAll(){
        List<Pharmaceutist> data = pharmaceutistService.findAll();
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("/update")
    public JsonResult<Void> update(Pharmaceutist pharmaceutist){
        pharmaceutistService.update(pharmaceutist);
        return new JsonResult<>(OK);
    }

    @RequestMapping("/delete")
    public JsonResult<Void> delete(int ph_ID){
        pharmaceutistService.delete(ph_ID);
        return new JsonResult<>(OK);
    }
}
